package com.buer.storage.service.impl;

import com.buer.storage.dao.CarDao;
import com.buer.storage.dao.CarprodstockdetailsDao;
import com.buer.storage.dao.StockDao;
import com.buer.storage.dao.StockdetailsDao;
import com.buer.storage.dto.CarDto;
import com.buer.storage.dto.OutCarprodToAreaDto;
import com.buer.storage.dto.OutStockToCarDto;
import com.buer.storage.dto.SelectCarNumDto;
import com.buer.storage.entity.*;
import com.buer.storage.service.CarService;
import com.buer.storage.utils.CheckEmptyUtil;
import com.buer.storage.vo.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CarServiceImpl implements CarService {
    @Resource
    private CarDao carDao;

    @Resource
    private StockDao stockDao;

    @Resource
    private CarprodstockdetailsDao carprodstockdetailsDao;

    @Autowired
    private StockdetailsDao stockdetailsDao;


    @Override
    public int insertCar(Car car) {
        return carDao.insertCar(car);
    }

    @Override
    public int deleteCarById(Integer carId) {
        return carDao.deleteCarById(carId);
    }

    @Override
    public int updateCarById(Car car) {
        return carDao.updateCarById(car);
    }

    @Override
    public List<CarDto> selectCarAll() {
        return carDao.selectCarAll();
    }

    @Override
    public PageInfo<Car> getCar(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Car> list = carDao.getCar(map); // 获取数据
        PageInfo<Car> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }


    @Override
    @Transactional
        public R submitAddCarprod(List<OutStockToCarDto> outStockToCarDtos) {

        if(outStockToCarDtos!=null&&outStockToCarDtos.size()>0){
            Map<String,Object> ifMap=new HashMap<>();
            for (OutStockToCarDto stockToCarDto:outStockToCarDtos){
                Stock stock = stockDao.get(stockToCarDto.getStockId());
                //1、先入库buer_carprod
                ifMap.put("prodCode",stock.getProdCode());
                ifMap.put("carid",stockToCarDto.getCarid());

                Carprod carprodByCode = carDao.getCarprodByCode(ifMap);
                /*if(stockToCarDto.getNum()>stock.getProdStocknum()){
                    return R.setR(0,"库存装备不足",null);
                }*/
                if (carprodByCode==null){
                    //添加
                    Carprod carprod=new Carprod();
                    carprod.setProdCode(stock.getProdCode());
                    carprod.setEmit(stock.getEmit());
                    carprod.setProdName(stock.getProdName());
                    carprod.setProdSpecial(stock.getProdSpecial());
                    carprod.setProdCategoryTwo(stock.getCategoryTwoId());
                    carprod.setNum(stockToCarDto.getNum());
                    carprod.setCarid(stockToCarDto.getCarid());
                    carDao.save(carprod);
                    //添加  车辆装备-库存详情关联表
                    if(CheckEmptyUtil.isNotEmpty(stockToCarDto.getStockDetails())){
                        for (Integer stockDetails:stockToCarDto.getStockDetails()){
                            //保存到carprodstockdetails
                            CarprodstockdetailsDO carprodstockdetailsDO=new CarprodstockdetailsDO();
                            carprodstockdetailsDO.setCarprodId(carprod.getCarprodId());
                            carprodstockdetailsDO.setStockdetailsId(stockDetails);
                            carprodstockdetailsDao.save(carprodstockdetailsDO);
                            //更新库存详情表stockDetail
                            stockdetailsDao.updateDetailStatusById(stockDetails);
                        }
                    }
                }else {
                    //更新
                    carprodByCode.setNum(carprodByCode.getNum()+stockToCarDto.getNum());
                    carDao.update(carprodByCode);

                    //添加  车辆装备-库存详情关联表
                    if(CheckEmptyUtil.isNotEmpty(stockToCarDto.getStockDetails())){
                        for (Integer stockDetails:stockToCarDto.getStockDetails()){
                            //保存到carprodstockdetails
                            CarprodstockdetailsDO carprodstockdetailsDO=new CarprodstockdetailsDO();
                            carprodstockdetailsDO.setCarprodId(carprodByCode.getCarprodId());
                            carprodstockdetailsDO.setStockdetailsId(stockDetails);
                            carprodstockdetailsDao.save(carprodstockdetailsDO);
                            //更新库存详情表stockDetail
                            stockdetailsDao.updateDetailStatusById(stockDetails);
                        }
                    }

                }
                //2、修改库存buer_stock数量
                stock.setProdStocknum(stock.getProdStocknum()-stockToCarDto.getNum());
                stock.setProdResiduenum(stock.getProdResiduenum()-stockToCarDto.getNum());
                stockDao.update(stock);
                if (stock.getProdStocknum()== 0){
                    stockDao.remove(stockToCarDto.getStockId());
                }
            }

        }

        return R.setR(1, "车载装备添加成功", null);
    }

    @Override
    @Transactional
    public R unloadDeleteCarprod(List<OutCarprodToAreaDto> outCarprodToAreaDtos) {
        if(outCarprodToAreaDtos!=null&&outCarprodToAreaDtos.size()>0){
            Map<String,Object> ifMap=new HashMap<>();
            for (OutCarprodToAreaDto carprodToAreaDto:outCarprodToAreaDtos){
                Carprod carprod = carDao.getCarprod(carprodToAreaDto.getCarprodId());
                //1、先入库buer_carprod
                ifMap.put("prodCode",carprod.getProdCode());
                ifMap.put("prodArea",carprodToAreaDto.getProdArea());
                ifMap.put("prodDepart",carprodToAreaDto.getProdDepart());
                Stock stockByCode = carDao.getStockByCode(ifMap);

                Area area = carDao.selectWarehouseId(carprodToAreaDto.getProdArea());

                if(stockByCode==null) {
                    Stock stock = new Stock();
                    stock.setProdCode(carprod.getProdCode());
                    stock.setProdName(carprod.getProdName());
                    stock.setProdSpecial(carprod.getProdSpecial());
                    stock.setCategoryTwoId(carprod.getProdCategoryTwo());
                    stock.setEmit(carprod.getEmit());
                    stock.setProdDate(carprodToAreaDto.getProdDate());
                    //System.out.println(carprodToAreaDto.getProdDate());
                    stock.setProdResiduenum(carprodToAreaDto.getNum());
                    stock.setProdAwaitnum(0);
                    stock.setWarehouseId(area.getWarehouseId());
                    stock.setProdArea(carprodToAreaDto.getProdArea());
                    stock.setProdDepart(carprodToAreaDto.getProdDepart());
                    stock.setProdStocknum(carprodToAreaDto.getNum());
                    stockDao.save(stock);

                }else {
                    //更新
                    stockByCode.setProdStocknum(stockByCode.getProdStocknum()+carprodToAreaDto.getNum());
                    stockByCode.setProdResiduenum(stockByCode.getProdResiduenum()+carprodToAreaDto.getNum());
                    stockDao.update(stockByCode);
                }

                //更新库存详情stockDetail
                if(CheckEmptyUtil.isNotEmpty(carprodToAreaDto.getStockDetails())) {
                    for (Integer stockDetails : carprodToAreaDto.getStockDetails()) {
                        StockdetailsDO stockdetailsDO = stockdetailsDao.get(stockDetails);
                        stockdetailsDO.setProdStatus(0);
                        stockdetailsDO.setProdArea(carprodToAreaDto.getProdArea());
                        stockdetailsDO.setProdDepart(carprodToAreaDto.getProdDepart());
                        stockdetailsDao.update(stockdetailsDO);

                        //删除 车辆装备-库存详情关联表信息
                        Map<String,Object> carprodstockdetailsMap=new HashMap<>();
                        carprodstockdetailsMap.put("carprodId",carprod.getCarprodId());
                        carprodstockdetailsMap.put("stockdetailsId",stockdetailsDO.getStockdetailsId());
                        carprodstockdetailsDao.removeByMap(carprodstockdetailsMap);
                    }
                }


                //修改库存carprod数量
                carprod.setNum(carprod.getNum()-carprodToAreaDto.getNum());
                carDao.update(carprod);
                if (carprod.getNum()==0) {
                    carDao.remove(carprodToAreaDto.getCarprodId());
                }
            }
        }

        return R.setR(1, "车载装备卸载成功", null);
    }

    @Override
    public List<Carprod> selectCarprodByCarid(Integer carId) {
       return carDao.selectCarprodByCarid(carId);
    }

    @Override
    public List<SelectCarNumDto> selectCarNum() {
        List<SelectCarNumDto> list = carDao.selectCarNum();
        return list;
    }

    @Override
    public List<CarDto> selectCarByName(String carNumber) {
       return carDao.selectCarByName(carNumber);
    }
}
